package com.ibm.spatiotemp.test.requestServer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.ibm.spatiotemp.tools.jsonParser;

/*json format request parser*/
public class JsonObject {

	/**
	 * Schema:<code>"0:parameters","1:resp.topic","2:resp.ip","3:resp.port"</code>
	 */
	public static String[] RequestTags = {"parameters","resp.topic","resp.ip","resp.port"};
	
	/**
	 * Schema:<code>"0:resptopic","1:content"</code>
	 */
	public static String[] ResponseTags = {"resptopic","content"};
	
	JsonNode jsonnode;
	public JsonObject(String jsonstr)
	{
		jsonnode = jsonParser.fromString(jsonstr);
	}
	
	public JsonObject(JsonNode jnode)
	{
		this.jsonnode = jnode;
	}
	
	public String getAttributeAsString(String tag){
		JsonNode child = jsonnode.get(tag);
		if(child.isValueNode())
			return child.asText();
		else
			return child.toString();
	}
	
	public int getAttributeAsInteger(String tag)
	{
		return jsonnode.get(tag).asInt();
	}
	
	public long getAttributeAsLong(String tag)
	{
		return jsonnode.get(tag).asLong();
	}
	
	public String[] getAttrbuteAsStringArray(String tag)
	{
		ArrayNode arraynode = (ArrayNode) jsonnode.get(tag);
		int arraylen = arraynode.size();
		String[] ret = new String[arraylen];
		for (int i = 0; i < arraylen; i++) {
			ret[i] = arraynode.get(i).asText();
		}
		return ret;
	}
}
